<?
/*
----------------------------------------
-----    Page logic description    -----
----------------------------------------
REFERENCES FROM:
    * cp__mon.php (as include)
NOTE:
    Refere to "cp_mon.php" for information
*/


if (in_array(@$maction, 
    array("pause", "activate", "remove")))
{
    $u = array();
    
    if ($maction == "pause") {
    	/* clear "active" bit in status word */
    	$u["status"] = "status & ~" . MONITOR_STATUS_IS_ACTIVE_MASK;
    }
    if ($maction == "activate") {
    	/* set "active" and clear "error" bits in status word */
    	$u["status"] = "(status | " . MONITOR_STATUS_IS_ACTIVE_MASK . ") & (~ ".MONITOR_STATUS_IS_ERROR_MASK.")";
    }
    
    if (count($u) > 0) {
    	$g_st_monitor->UpdateWithoutEncoding($u, array(@$mid));
    }
    
    if ($maction == "remove") {
    	$g_st_monitor->Delete(array(@$mid));
    }
    
    return;
}


if (! in_array(@$maction, 
    array("save", "resume_suspend")))
{
    return;
}

// Create monitor object
$monitor = new CMonitor();

// Load/Init monitor's data
if (@$mid > 0) {
    $monitor->LoadById(@$mid) or die("Incorrect data - wrong monitor id.");
}
else {
    /* create new monitor */
    $monitor->InitNew($account);
}


/*
if (@$maction == "resume_suspend")
{
	$res = $monitor->data["status"];
	
	if ($res & MONITOR_STATUS_IS_ACTIVE_MASK)
	{
		// active - clear "active" bit in status word
		$res &= ~ MONITOR_STATUS_IS_ACTIVE_MASK;
	}
	else
	{
		// inactive - set "active" and clear "error" bits in status word
		$res |= MONITOR_STATUS_IS_ACTIVE_MASK;
		$res &= ~ MONITOR_STATUS_IS_ERROR_MASK;
	}
	$monitor->Update(array("status" => $res));
	
	if (@$do_exit) return;
	generate_html_redirect("dashboard.php");
}
*/


if (@$maction == "save")
{
	// Process form data
	if ($f_category_new_name != "") {
		$f_category_id = $categories->CreateNew($f_category_new_name);
	}
	
	set_if_null($f_disable_top_list_stats, 0);

	$f_trigger_type = (int)$f_trigger_type;
	$f_trigger_type |= @$f_trigger_type_search_text;
	$f_trigger_type |= @$f_trigger_type_skip_digits;
	$f_trigger_type |= @$f_trigger_type_skip_sm_ch;
	
	$f_enable_history = isset($f_enable_history) ? 1 : 0;
	
	// Bug fix - temporary
	set_if_null($f_alert_format, ALERT_FORMAT_PLAIN_TEXT);
	
	$f_monitor_at_time = (int)$f_monitor_at_time;
	
	$f_alert_at_time = (int)$f_alert_at_time;
	if (@$f_alert_at_time_flag == "0") $f_alert_at_time = -1;
	
	$f_highlight_font_style = 0x00;
	foreach ($g_highlight_font_style_map as $k => $name) 
	{
		$t = 'f_hl_fs_'.$k;
		if (isset($$t)) $f_highlight_font_style |= $k;
	}
	
	$tmp = $monitor->data["status"];
	if ($f_status_is_active == 0) {
		/* new status "suspended" - clear "active" bit in status word */
		$tmp &= ~ MONITOR_STATUS_IS_ACTIVE_MASK;
	}
	else {
		/* new status "active" - set "active" and clear "error" bits in status word */
		$tmp |= MONITOR_STATUS_IS_ACTIVE_MASK;
		$tmp &= ~ MONITOR_STATUS_IS_ERROR_MASK;
	}
	$f_status = $tmp;
	
	/* TODO: Duplicated code - same as in members/cp_monitor_process.php */
	/* RESOLUTION: move to separate function, probably to c_monitor.php  */
	$f_alert_type = 0;
    set_if_null($f_alert_type_cb, array());
	foreach ($f_alert_type_cb as $k => $v)
	{
		$f_alert_type |= (int)$v;
	}
	if (isset($f_alert_type_feed)) {
		$f_alert_type |= $f_alert_type_feed_cb;
	}
	
	
	get_form_values($values, array(
		"url", "title", "category_id", "frequency", "monitor_at_time", "alert_at_time",
		"disable_top_list_stats",
		"xpath", "trigger_type", "trigger_params",
		"alert_type", "alert_emails", "status",
		"reference_url", "enable_history",
		"highlight_font_color", "highlight_bk_color", "highlight_font_style",
        "user_charset", "http_user_agent_type", "http_user_agent_custom",
		"http_auth_user", "http_auth_password",
		"http_cookies", "http_form_content",
		"proxy_addr", "proxy_port",
		"proxy_auth_user", "proxy_auth_password",
	));
}

// URL normalization
if (isset($values) && isset($values["url"]))
{
	$values["url"] = normalize_url($values["url"]);
}


if (@$maction == "save") 
{
    if ($monitor->id > 0) {
	    $monitor->Update($values);
    }
    else {
	    $monitor->CreateNew($account, $values);
        $_REQUEST["mid"] = $monitor->id;
    }
}
else 
{
	die("Incorrect data - maction.");
}

// Save transparent settings to defaults in account
$account->SaveTransparentSettings($monitor);
	
$warn = "";
// Check if monitor with such URL already exists
if ($monitor->CheckIfExist())
{
    // TODO
	if ($warn == "") $warn .= "?"; else $warn .= "&";
	$warn .= "warn=already_exists";
}

?>